<?php
function edit_bckabout($config,$param){
	$desc = trim($param['descabout']);
	if ($desc!=""){
		$db = new db($config);
		$que = "UPDATE `deskripsi` SET `deskripsi`='$desc' WHERE `page` = 'aboutus';";
		$db->exec($que);
		$_SESSION['editted'] = 'Sukses edit deskripsi about us';
	}
	header('Location: '.$config['home_url']."backend/aboutus");
}

function add_bckaboutangg($config,$param){
	$nama = trim($param['namaplus']);
	$fb = trim($param['fbplus']);
	$email = trim($param['emplus']);
	$kontak = trim($param['kontakplus']);
	$title = trim($param['titplus']);
	$desc = trim($param['descplus']);
	//echo $kutip;
	if ($nama!=""){
		$db = new db($config);
		$que = "INSERT INTO `aboutus` (`nama` ,`idFB` ,`email` ,`kontak` ,`title`, `desc`) VALUES (\"$nama\", \"$fb\", \"$email\", \"$kontak\", \"$title\", \"$desc\");";
		//echo $que; die();
		$db->exec($que);
		if ($db->lastInsertId()>0){
			//PP
			if ($_FILES['ppplus']['name']!=""){
				$uploaddir = $config['image_path'].'about-us'.DIRECTORY_SEPARATOR.'pp'.DIRECTORY_SEPARATOR;
				$id = $db->lastInsertId();
				$limitedext = array("jpg","jpeg","png","bmp");
				$ext = strrchr($_FILES['ppplus']['name'],'.');
				$ext = strtolower($ext);
				$getExt = explode('.',$ext);
				$file_ext = $getExt[count($getExt)-1];
				$file_name = $id.".".$file_ext;
				$uploadfile = $uploaddir.$file_name;
				if (!in_array($file_ext,$limitedext)) {
					$_SESSION['editted'] = "Tipe file salah<br />Tipe file yang diperbolehkan adalah jpg, jpeg, bmp, png.";
				} else {
					if (move_uploaded_file($_FILES['ppplus']['tmp_name'], $uploadfile)) {
						// process for thumbnail image
						WideImage::load($uploadfile)->resize(160,160,'fill')->saveToFile($uploadfile);	//pp
						$_SESSION['editted'] = "Sukses upload profpict";
					} else {
						switch ($_FILES['ppplus']['error']) {
							case 2:
								$_SESSION['editted'] = "Error: Ukuran gambar melebihi batas<br />Batas ukuran file adalah 1 MB";
								break;
							case 7:
								$_SESSION['editted'] = "Error: Tidak dapat disimpan dalam server";
								break;
							default:
								$_SESSION['editted'] = "Error: Unknown error ".$_FILES['ppplus']['error'];
						}
					}
				}
			}
			//ANGGOTA
			if ($_FILES['anggplus']['name']!=""){
				$uploaddir = $config['image_path'].'about-us'.DIRECTORY_SEPARATOR.'anggota'.DIRECTORY_SEPARATOR;
				$id = $db->lastInsertId();
				$limitedext = array("jpg","jpeg","png","bmp");
				$ext = strrchr($_FILES['anggplus']['name'],'.');
				$ext = strtolower($ext);
				$getExt = explode('.',$ext);
				$file_ext = $getExt[count($getExt)-1];
				$file_name = $id.".".$file_ext;
				$uploadfile = $uploaddir.$file_name;
				if (!in_array($file_ext,$limitedext)) {
					$_SESSION['editted'] = "Tipe file salah<br />Tipe file yang diperbolehkan adalah jpg, jpeg, bmp, png.";
				} else {
					if (move_uploaded_file($_FILES['anggplus']['tmp_name'], $uploadfile)) {
						// process for thumbnail image
						WideImage::load($uploadfile)->resize(160,160,'fill')->saveToFile($uploadfile);	//anggota
						$_SESSION['editted'] = "Sukses upload profpict";
					} else {
						switch ($_FILES['anggplus']['error']) {
							case 2:
								$_SESSION['editted'] = "Error: Ukuran gambar melebihi batas<br />Batas ukuran file adalah 1 MB";
								break;
							case 7:
								$_SESSION['editted'] = "Error: Tidak dapat disimpan dalam server";
								break;
							default:
								$_SESSION['editted'] = "Error: Unknown error ".$_FILES['anggplus']['error'];
						}
					}
				}
			}
			$_SESSION['editted'] = 'Sukses tambah anggota';
		}else
			$_SESSION['editted'] = 'Gagal tambah anggota';
	}
	header('Location: '.$config['home_url']."backend/aboutus");
}

function edit_bckaboutangg($config,$param){
	$nama = trim($param['namaped']);
	$fb = trim($param['fbed']);
	$email = trim($param['emed']);
	$kontak = trim($param['kontaked']);
	$title = trim($param['tited']);
	$desc = trim($param['desced']);
	$idk = $param['id'];
	//echo $kutip." ".$idk;
	if ($nama!=""){
		$db = new db($config);
		$que = "UPDATE `aboutus` SET `nama`=\"$nama\", `idFB`=\"$fb\", `email`=\"$email\", `kontak`=\"$kontak\", `title`=\"$title\", `desc`=\"$desc\" WHERE `idAbout` = $idk;";
		echo $que;
		$db->exec($que);
		$_SESSION['editted'] = 'Sukses edit anggota '.$nama;
	}
	header('Location: '.$config['home_url']."backend/aboutus");
}

function del_bckaboutangg($config){
	$idk = $config['ruri'][2];
	$db = new db($config);
	$que = "DELETE FROM aboutus WHERE idAbout=$idk";
	echo $que;
	$db->exec($que);
	//PP
	$ppdir = $config['image_path'].'about-us'.DIRECTORY_SEPARATOR.'pp'.DIRECTORY_SEPARATOR;
	$regex2 = $ppdir.$idk.".*";
	$limitedext = array("jpg","jpeg","png","bmp");
	//FILTER
	foreach (glob($regex2) as $files) {
		$getFile = explode(DIRECTORY_SEPARATOR,$files);
		$file = $getFile[count($getFile)-1];
		unlink($ppdir.$file);
	}
	//PP
	$anggdir = $config['image_path'].'about-us'.DIRECTORY_SEPARATOR.'anggota'.DIRECTORY_SEPARATOR;
	$regex2 = $anggdir.$idk.".*";
	$limitedext = array("jpg","jpeg","png","bmp");
	//FILTER
	foreach (glob($regex2) as $files) {
		$getFile = explode(DIRECTORY_SEPARATOR,$files);
		$file = $getFile[count($getFile)-1];
		unlink($anggdir.$file);
	}
	$_SESSION['editted'] = 'Sukses hapus anggota';
	header('Location: '.$config['home_url']."backend/aboutus");
}

function add_bckaboutfoto($config,$param){
	$uploaddir = $config['image_path'].'about-us'.DIRECTORY_SEPARATOR.'slide'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'about-us'.DIRECTORY_SEPARATOR.'pre'.DIRECTORY_SEPARATOR;
	$limitedext = array("jpg","jpeg","png","bmp");
	$ext = strrchr($_FILES['gambar']['name'],'.');
	$ext = strtolower($ext);
	$getExt = explode('.',$ext);
	$getName = explode('.',$_FILES['gambar']['name']);
	$getName = helper::slugging($getName[0]);
	$file_ext = $getExt[count($getExt)-1];
	$file_name = $getName.".".$file_ext;
	$uploadfile = $uploaddir.$file_name;
	if (!in_array($file_ext,$limitedext)) {
		$_SESSION['editted'] = "Tipe file salah<br />Tipe file yang diperbolehkan adalah jpg, jpeg, bmp, png.";
	} else {
		echo $_FILES['gambar']['tmp_name'];
		echo "\n".$uploadfile;
		if (move_uploaded_file($_FILES['gambar']['tmp_name'], $uploadfile)) {
			// process for thumbnail image
//			$file_name = $_FILES['gambar']['name'];
			echo "\n".$file_name;
			WideImage::load($uploadfile)->resize(790,250,'fill')->saveToFile($uploadfile);	//slide
			WideImage::load($uploadfile)->resize(154,50,'fill')->saveToFile($preddir.$file_name);	//pre
			//WideImage::load($uploadfile)->crop('center','center',250,300)->saveToFile($preddir.$file_name.'.'.$file_ext);	//pre
			//unlink($uploadfile);
			$_SESSION['editted'] = "Sukses upload gambar";
		} else {
			switch ($_FILES['gambar']['error']) {
				case 2:
					$_SESSION['editted'] = "Error: Ukuran gambar melebihi batas<br />Batas ukuran file adalah 1 MB";
					break;
				case 7:
					$_SESSION['editted'] = "Error: Tidak dapat disimpan dalam server";
					break;
				default:
					$_SESSION['editted'] = "Error: Unknown error ".$_FILES['gambar']['error'];
			}
		}
	}
	//die();
	header('Location: '.$config['home_url']."backend/aboutus");
}

function del_bckaboutfoto($config,$param){
	$img=$param['img'];
	$uploaddir = $config['image_path'].'about-us'.DIRECTORY_SEPARATOR.'slide'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'about-us'.DIRECTORY_SEPARATOR.'pre'.DIRECTORY_SEPARATOR;
	unlink($uploaddir.$img);
	unlink($preddir.$img);
	$_SESSION['editted'] = 'Sukses hapus foto';
	header('Location: '.$config['home_url']."backend/aboutus");
}

function add_bckaboutanggpp($config,$param){
	$uploaddir = $config['image_path'].'about-us'.DIRECTORY_SEPARATOR.'pp'.DIRECTORY_SEPARATOR;
	$id = $param['idk'];
	$limitedext = array("jpg","jpeg","png","bmp");
	$ext = strrchr($_FILES['pp']['name'],'.');
	$ext = strtolower($ext);
	$getExt = explode('.',$ext);
	$file_ext = $getExt[count($getExt)-1];
	$file_name = $id.".".$file_ext;
	$uploadfile = $uploaddir.$file_name;
	echo $uploadfile;
	if (!in_array($file_ext,$limitedext)) {
		$_SESSION['editted'] = "Tipe file salah<br />Tipe file yang diperbolehkan adalah jpg, jpeg, bmp, png.";
	} else {
		if (move_uploaded_file($_FILES['pp']['tmp_name'], $uploadfile)) {
			// process for thumbnail image
			WideImage::load($uploadfile)->resize(160,160,'fill')->saveToFile($uploadfile);	//works
			$_SESSION['editted'] = "Sukses upload foto profil";
		} else {
			switch ($_FILES['pp']['error']) {
				case 2:
					$_SESSION['editted'] = "Error: Ukuran gambar melebihi batas<br />Batas ukuran file adalah 1 MB";
					break;
				case 7:
					$_SESSION['editted'] = "Error: Tidak dapat disimpan dalam server";
					break;
				default:
					$_SESSION['editted'] = "Error: Unknown error ".$_FILES['pp']['error'];
			}
		}
	}
	header('Location: '.$config['home_url']."backend/aboutus");
}

function del_bckaboutanggpp($config,$param){
	$file=$param['img'];
	$uploaddir = $config['image_path'].'about-us'.DIRECTORY_SEPARATOR.'pp'.DIRECTORY_SEPARATOR;
	unlink($uploaddir.$file);
	$_SESSION['editted'] = 'Sukses hapus pp';
	header('Location: '.$config['home_url']."backend/aboutus");
}


function add_bckaboutanggfoto($config,$param){
	$uploaddir = $config['image_path'].'about-us'.DIRECTORY_SEPARATOR.'anggota'.DIRECTORY_SEPARATOR;
	$id = $param['idk'];
	$limitedext = array("jpg","jpeg","png","bmp");
	$ext = strrchr($_FILES['pp']['name'],'.');
	$ext = strtolower($ext);
	$getExt = explode('.',$ext);
	$file_ext = $getExt[count($getExt)-1];
	$file_name = $id.".".$file_ext;
	$uploadfile = $uploaddir.$file_name;
	echo $uploadfile;
	if (!in_array($file_ext,$limitedext)) {
		$_SESSION['editted'] = "Tipe file salah<br />Tipe file yang diperbolehkan adalah jpg, jpeg, bmp, png.";
	} else {
		if (move_uploaded_file($_FILES['pp']['tmp_name'], $uploadfile)) {
			// process for thumbnail image
			WideImage::load($uploadfile)->resize(160,160,'fill')->saveToFile($uploadfile);	//anggota
			$_SESSION['editted'] = "Sukses upload foto anggota";
		} else {
			switch ($_FILES['pp']['error']) {
				case 2:
					$_SESSION['editted'] = "Error: Ukuran gambar melebihi batas<br />Batas ukuran file adalah 1 MB";
					break;
				case 7:
					$_SESSION['editted'] = "Error: Tidak dapat disimpan dalam server";
					break;
				default:
					$_SESSION['editted'] = "Error: Unknown error ".$_FILES['pp']['error'];
			}
		}
	}
	header('Location: '.$config['home_url']."backend/aboutus");
}

function del_bckaboutanggfoto($config,$param){
	$file=$param['img'];
	$uploaddir = $config['image_path'].'about-us'.DIRECTORY_SEPARATOR.'anggota'.DIRECTORY_SEPARATOR;
	unlink($uploaddir.$file);
	$_SESSION['editted'] = 'Sukses hapus foto anggota';
	header('Location: '.$config['home_url']."backend/aboutus");
}
